﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.Sakila.TestSite.Objects;
using Acme.Sakila.EFDAL;

namespace Acme.Sakila.TestSite
{
	public partial class CategoryItem : BasePersistablePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			cmdSave.Click += new EventHandler(SaveData);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
		}

		#endregion

		#region Methods

		protected override nHydrate.EFCore.DataAccess.NHEntityObject CreateObject(SakilaEntities context)
		{
			var newItem = new Acme.Sakila.EFDAL.Entity.category();
			context.AddItem(newItem);
			return newItem;
		}

		protected override void SetupBindings(SakilaEntities context)
		{
			var categoryId = this.Request["id"].ToInt32();
			var category = context.category.FirstOrDefault(x => x.category_id == categoryId);
			if (category == null)
				category = CreateObject(context) as Acme.Sakila.EFDAL.Entity.category;

			Mapper.Map(txtName, category, Acme.Sakila.EFDAL.Entity.category.FieldNameConstants.name, lblName);
			Mapper.Map(lblCategoryId, category, Acme.Sakila.EFDAL.Entity.category.FieldNameConstants.category_id);
		}

		#endregion

		#region Event Handlers

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.Response.Redirect(SessionHelper.LastCategoryListSearch);
		}

		protected override void OnDataSavedSucess(EventArgs e)
		{
			base.OnDataSavedSucess(e);
			this.Response.Redirect(SessionHelper.LastCategoryListSearch);
		}

		#endregion

	}
}